package com.edu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/*
 * @Author:princess
 * @Date 2023/11/16 11 16 9:43
 *  @SessionAttributes("变量名")
 *      放在类上
 *      a.当前Controller里的所有方法，再向Model中存入msg的时候，都会一起向session中存一份
 *      b.当前所有的方法在执行的时候，都会尝试从session中取出msg并存入Model
 */
@Controller
@SessionAttributes("msg")
public class SessionController {
//    @Resource
//    private HttpSession session;
    @RequestMapping("session01")
    public String session01(HttpSession session){
        session.setAttribute("msg","通过SevletAPI向session中存入的数据");
        return "index3";
    }

    @RequestMapping("session02")
    public String session02(Model model){
        model.addAttribute("msg","通过Model向session中存入的数据");
        return "index3";
    }

    @RequestMapping("session03")
    public String session03(Model model){
        return "index3";
    }

    @RequestMapping("session04")
    public String session04(@SessionAttribute(value = "msg",required = false) String msg){
        System.out.println(msg);
        return "index3";
    }
}
